//获取应用实例
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  onAuth: function () {
    wx.login({
      success(res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://www.2edge.cn/rest/wx/login',
            method: "POST",
            data: {
              code: res.code,
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            success: function (res) {
              var cookie = res.header["Set-Cookie"];
              if (cookie != null) {
                wx.setStorageSync("sessionid", res.header["Set-Cookie"]);
              }
              // console.log(res.data);
              if (res.data.code === -4 && res.data.status === false) {
                let id = res.data.data;
                console.log(id);
                wx.setStorage({
                  key: "openid",
                  data: id
                })
                wx.redirectTo({ url: '/pages/login/login' })
              } else if (res.data.status) {
                let token = res.data.data.token
                // console.log(res);
                wx.setStorage({
                  key: "openid",
                  data: res.data.data.openid
                })

                wx.setStorage({
                  key: "token",
                  data: token
                })
                wx.redirectTo({ url: '/pages/home/home' })
              } else {
                wx.showToast({
                  title: res.data.message,
                  icon: 'none',
                  duration: 2000
                })
              }

            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  }

})

